package com.dg.gaming.server;

import java.util.concurrent.*;

import com.dg.gaming.server.messages.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class WorkerQueue /*implements Runnable*/ {
//    LinkedBlockingQueue<AUserMessage> messagequeue = null;
    ThreadPoolExecutor executor = null;
    boolean alive;

    public WorkerQueue(int messageQueueSize, int threadqueuecapacity) {
//        messagequeue = new LinkedBlockingQueue<AUserMessage>(
//                messageQueueSize);
        executor = new ThreadPoolExecutor(threadqueuecapacity,
                                          threadqueuecapacity, 100,
                                          TimeUnit.MILLISECONDS,
                                          new ArrayBlockingQueue(5));
//        new Thread(this).start();
        alive = true;

    }

    public void addMessage(AUserMessage usermassage) {
        if (isAlive()) {
            AUserMessage message = usermassage;
            Worker worker = new Worker(this, message);
            executor.execute(worker);

        }
//        messagequeue.add(usermassage);

    }

    public boolean isAlive() {
        return alive;
    }

    public void setAlive(boolean alive) {
        this.alive = alive;
        if (alive) {
            return;

        }
    }

    public void run() {
//        while (isAlive()) {
//            try {
//                AUserMessage message = messagequeue.take();
//                if (message == null) {
//                    continue;
//                }
//                Worker worker = new Worker(this, message);
//                executor.execute(worker);
//            } catch (InterruptedException ex) {
//            }
//        }
    }

}
